package pcjavanet.mobilesell.service;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;  
import java.util.Iterator; 
import java.util.Map;      
import pcjavanet.mobilesell.model.Shop;
import pcjavanet.mobilesell.model.ShopExample;
import pcjavanet.mobilesell.model.Counter; 
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.CounterExample;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;
import pcjavanet.mobilesell.persistence.ShopMapper;
import pcjavanet.mobilesell.persistence.CounterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class CounterService {


  @Autowired
  private  CounterMapper counterMapper;
  @Autowired
  private  ShopMapper shopMapper;


  public int  insert( Counter counter ) throws Exception{
	return counterMapper.insert (counter);
  }


  public Counter  selectByPrimaryKey( Counter  bean ) {
 	int dbid = bean.getDbid();
	Counter counter = counterMapper.selectByPrimaryKey( dbid );
	Shop shop = shopMapper.selectByPrimaryKey( counter.getRefShopDbid() );
	counter.setShop(shop) ;
	return counter ;
  }


  public int  updateByPrimaryKeySelective( Counter counter ) throws Exception{
	return counterMapper.updateByPrimaryKeySelective( counter );
  }


  public int  deleteByPrimaryKey( Counter  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return counterMapper.deleteByPrimaryKey( dbid );
  }


  public List<Counter>  selectByExample( Counter counter ) throws Exception{
    CounterExample example = new CounterExample();
	return counterMapper.selectByExample( example );
  }

  public List<Counter>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    CounterExample example = new CounterExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<Counter> ls=counterMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> shopIdmap = new HashMap<Integer,Integer>();
  		for(Counter  m : ls){
  			m.setShop(new Shop ()) ; 
  			if ( m.getRefShopDbid() != null ) 
  				shopIdmap.put(m.getRefShopDbid(), m.getRefShopDbid());
  		};
  		List<Integer> refShopDbids = new ArrayList<Integer>();
  		Iterator<Integer> refShopDbidit = shopIdmap.keySet().iterator(); 
  		while ( refShopDbidit.hasNext() ){
  			refShopDbids.add( refShopDbidit.next() );
  		}
  		ShopExample shopExample = new ShopExample();
  		if ( refShopDbids.size() > 0 ) 
  			shopExample.createCriteria().andDbidIn( refShopDbids );
  		List<Shop> shops = shopMapper.selectByExample( shopExample);
  		for(Counter m : ls ){
  			for(Shop c : shops ){
  				if ( m.getRefShopDbid() !=null && c.getDbid() !=null && m.getRefShopDbid().intValue() == c.getDbid().intValue() ) {
  					m.setShop( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    CounterExample example = new CounterExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return counterMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Counter counter , PageBean pageBean ){
    CounterExample example = new CounterExample();
	return counterMapper.countByExample( example );
  }
  public List<Counter> selectByExamplePage( Counter counter , PageBean pageBean ){
  CounterExample example = new CounterExample();
	return counterMapper.selectByExamplePage( example , pageBean );
  }

}
